home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GSMISC.C < prev    next >
C/C++ Source or Header  |  1993-05-28  |  10KB  |  329 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsmisc.c */
  20. /* Miscellaneous utilities for Ghostscript library */
  21. #include "gx.h"
  22. #include "errno_.h"
  23. #include "malloc_.h"
  24. #include "memory_.h"
  25.  
  26. /* Define private replacements for stdin, stdout, and stderr. */
  27. FILE *gs_stdin, *gs_stdout, *gs_stderr;
  28. /* Ghostscript writes debugging output to gs_debug_out. */
  29. /* We define gs_debug and gs_debug_out even if DEBUG isn't defined, */
  30. /* so that we can compile individual modules with DEBUG set. */
  31. char gs_debug[128];
  32. FILE *gs_debug_out;
  33. /* We define gs_log_errors here for the same reason. */
  34. int gs_log_errors = 0;
  35.  
  36. /* We can turn on allocator debugging even if DEBUG isn't defined. */
  37. int gs_alloc_debug = 0;
  38. byte gs_alloc_fill_alloc = 0xa1;
  39. byte gs_alloc_fill_free = 0xf1;
  40.  
  41. /* ------ Substitutes for missing C library functions ------ */
  42.  
  43. #ifdef memory__need_memset        /* see memory_.h */
  44. void
  45. memset(void *dest, register char ch, uint len)
  46. {    if ( ch == 0 )
  47.         bzero(dest, len);
  48.     else if ( len > 0 )
  49.     {    register char *p = dest;
  50.         register uint count = len;
  51.         do { *p++ = ch; } while ( --count );
  52.     }
  53. }
  54. #endif
  55.  
  56. #ifdef memory__need_memchr        /* see memory_.h */
  57. const char *
  58. memchr(const char *ptr, char ch, uint len)
  59. {    if ( len > 0 )
  60.     {    register const char *p = ptr;
  61.         register uint count = len;
  62.         do { if ( *p == ch ) return p; p++; } while ( --count );
  63.     }
  64.     return 0;
  65. }
  66. #endif
  67.  
  68. /* ------ Ghostscript utilities ------ */
  69.  
  70. /* Generate a block of unique IDs. */
  71. static ulong gs_next_id = 0;
  72. ulong
  73. gs_next_ids(uint count)
  74. {    ulong id;
  75.     if ( gs_next_id == 0 ) gs_next_id++;
  76.     id = gs_next_id;
  77.     gs_next_id += count;
  78.     return id;
  79. }
  80.  
  81. /*
  82.  * Versions of malloc and free compatible with Ghostscript's
  83.  * model of memory management.  We keep track of all allocated
  84.  * blocks so we can free them at cleanup time.
  85.  */
  86. const gs_memory_procs gs_default_memory_procs = {
  87.     gs_malloc,
  88.     gs_free
  89. };
  90. /* We must make sure that malloc_blocks leave the block aligned. */
  91. typedef struct malloc_block_s malloc_block;
  92. #define malloc_block_data\
  93.     malloc_block *next;\
  94.     uint size;\
  95.     const char *cname
  96. struct malloc_block_data_s { malloc_block_data; };
  97. struct malloc_block_s {
  98.     malloc_block_data;
  99. /* ANSI C does not allow zero-size arrays, so we need the following */
  100. /* unnecessary and wasteful workaround: */
  101. #define _npad (-sizeof(struct malloc_block_data_s) & 7)
  102.     byte _pad[(_npad == 0 ? 8 : _npad)];    /* pad to double */
  103. #undef _npad
  104. };
  105. private malloc_block *malloc_list = 0;
  106. char *
  107. gs_malloc(uint num_elts, uint elt_size, const char *client_name)
  108. {    char *ptr;
  109.     const char *msg = "";
  110.     if ( elt_size != 0 &&
  111.          num_elts > (max_uint - sizeof(malloc_block)) / elt_size
  112.        )
  113.        {    /* Can't represent the size in a uint! */
  114.         msg = "too large for size_t";
  115.         ptr = 0;
  116.        }
  117.     else
  118.        {    uint size = num_elts * elt_size;
  119.         ptr = malloc(size + sizeof(malloc_block));
  120.         if ( ptr == 0 )
  121.             msg = "failed";
  122.         else
  123.            {    malloc_block *bp = (malloc_block *)ptr;
  124.             bp->next = malloc_list;
  125.             bp->size = size;
  126.             bp->cname = client_name;
  127.             malloc_list = bp;
  128.             msg = "OK";
  129.             ptr = (char *)(bp + 1);
  130.             if ( gs_alloc_debug )
  131.               { /* Clear the block in an attempt to track down */
  132.                 /* uninitialized data errors. */
  133.                 memset(ptr, gs_alloc_fill_alloc, size);
  134.               }
  135.            }
  136.        }
  137.     if ( gs_alloc_debug || !*msg )
  138.         dprintf5("gs_malloc(%s)(%u, %u) = 0x%lx: %s\n", client_name,
  139.              num_elts, elt_size, (ulong)ptr, msg);
  140.     return ptr;
  141. }
  142. void
  143. gs_free(char *ptr, uint num_elts, uint elt_size, const char *client_name)
  144. {    malloc_block *bp = malloc_list;
  145.     if ( gs_alloc_debug )
  146.         dprintf4("gs_free(%s)(0x%lx, %u, %u)\n", client_name,
  147.              (ulong)ptr, num_elts, elt_size);
  148.     if ( ptr == (char *)(bp + 1) )
  149.       {
  150. #ifdef DEBUG
  151.         if ( bp->size != num_elts * elt_size )
  152.           lprintf5("%s: free 0x%lx(%u,%u) size ~= %u\n",
  153.                client_name, (ulong)ptr, num_elts, elt_size,
  154.                bp->size);
  155. #endif
  156.         malloc_list = bp->next;
  157.         if ( gs_alloc_debug )
  158.           memset((char *)(bp + 1), gs_alloc_fill_free, bp->size);
  159.         free(bp);
  160.       }
  161.     else
  162.       { malloc_block *np;
  163.         for ( ; (np = bp->next) != 0; bp = np )
  164.           { if ( ptr == (char *)(np + 1) )
  165.           {
  166. #ifdef DEBUG
  167.             if ( np->size != num_elts * elt_size )
  168.               lprintf5("%s: free 0x%lx(%u,%u) size ~= %u\n",
  169.                    client_name, (ulong)ptr, num_elts, elt_size,
  170.                    np->size);
  171. #endif
  172.             bp->next = np->next;
  173.             if ( gs_alloc_debug )
  174.               memset((char *)(np + 1), gs_alloc_fill_free, np->size);
  175.             free(np);
  176.             return;
  177.           }
  178.           }
  179.         lprintf4("%s: free 0x%lx(%u,%u) not found\n",
  180.              client_name, (ulong)ptr, num_elts, elt_size);
  181.         free((char *)((malloc_block *)ptr - 1));
  182.       }
  183. }
  184. void
  185. gs_malloc_release(void)
  186. {    malloc_block *bp = malloc_list;
  187.     malloc_block *np;
  188.     for ( ; bp != 0; bp = np )
  189.        {    np = bp->next;
  190.         if ( gs_alloc_debug )
  191.           memset((char *)(bp + 1), gs_alloc_fill_free, bp->size);
  192.         free(bp);
  193.        }
  194.     malloc_list = 0;
  195. }
  196.  
  197. /* Transpose an 8 x 8 block of bits.  line_size is the raster of */
  198. /* the input data.  dist is the distance between output bytes. */
  199. /* This routine may be supplanted by assembly code. */
  200. #if !USE_ASM
  201.  
  202. #if 1        /* This is the better of the two algorithms. */
  203.  
  204. void
  205. memflip8x8(const byte *inp, int line_size, byte *outp, int dist)
  206. {    register uint ae, bf, cg, dh;
  207.        {    const byte *ptr4 = inp + (line_size << 2);
  208.         ae = ((uint)*inp << 8) + *ptr4;
  209.         inp += line_size, ptr4 += line_size;
  210.         bf = ((uint)*inp << 8) + *ptr4;
  211.         inp += line_size, ptr4 += line_size;
  212.         cg = ((uint)*inp << 8) + *ptr4;
  213.         inp += line_size, ptr4 += line_size;
  214.         dh = ((uint)*inp << 8) + *ptr4;
  215.        }
  216.  
  217.     /* Check for all 8 bytes being the same. */
  218.     /* This is especially worth doing for the case where all are zero. */
  219.     if ( ae == bf && ae == cg && ae == dh && (ae >> 8) == (ae & 0xff) )
  220.        {    if ( ae == 0 ) goto store;
  221.         *outp = -((ae >> 7) & 1);
  222.         outp += dist;
  223.         *outp = -((ae >> 6) & 1);
  224.         outp += dist;
  225.         *outp = -((ae >> 5) & 1);
  226.         outp += dist;
  227.         *outp = -((ae >> 4) & 1);
  228.         outp += dist;
  229.         *outp = -((ae >> 3) & 1);
  230.         outp += dist;
  231.         *outp = -((ae >> 2) & 1);
  232.         outp += dist;
  233.         *outp = -((ae >> 1) & 1);
  234.         outp += dist;
  235.         *outp = -(ae & 1);
  236.         return;
  237.        }
  238.  
  239.        {    register uint temp;
  240.  
  241. /* Transpose a block of bits between registers. */
  242. #define transpose(r,s,mask,shift)\
  243.   r ^= (temp = ((s >> shift) ^ r) & mask);\
  244.   s ^= temp << shift
  245.  
  246. /* Transpose blocks of 4 x 4 */
  247. #define transpose4(r) transpose(r,r,0x00f0,4)
  248.     transpose4(ae);
  249.     transpose4(bf);
  250.     transpose4(cg);
  251.     transpose4(dh);
  252.  
  253. /* Transpose blocks of 2 x 2 */
  254.     transpose(ae, cg, 0x3333, 2);
  255.     transpose(bf, dh, 0x3333, 2);
  256.  
  257. /* Transpose blocks of 1 x 1 */
  258.     transpose(ae, bf, 0x5555, 1);
  259.     transpose(cg, dh, 0x5555, 1);
  260.  
  261.        }
  262.  
  263. store:    *outp = ae >> 8;
  264.     outp += dist;
  265.     *outp = bf >> 8;
  266.     outp += dist;
  267.     *outp = cg >> 8;
  268.     outp += dist;
  269.     *outp = dh >> 8;
  270.     outp += dist;
  271.     *outp = (byte)ae;
  272.     outp += dist;
  273.     *outp = (byte)bf;
  274.     outp += dist;
  275.     *outp = (byte)cg;
  276.     outp += dist;
  277.     *outp = (byte)dh;
  278. }
  279.  
  280. #else        /* This looked like a good idea, but it's no faster. */
  281.  
  282. /* Transpose an 8 x 8 block of bits.  line_size is the raster of */
  283. /* the input data.  dist is the dis